package cn.edu.xhu.library_management.area.controller;

import cn.edu.xhu.library_management.area.entity.LmBookRack;
import cn.edu.xhu.library_management.area.entity.dto.BookRackLocationDTO;
import cn.edu.xhu.library_management.area.entity.search.LmBookRackSearch;
import cn.edu.xhu.library_management.area.entity.vo.LmBookRackVo;
import cn.edu.xhu.library_management.area.service.BookRackService;
import cn.edu.xhu.library_management.common.entity.R;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: MMJ
 * @Date: 2022 - 11 - 19
 * @Description: 书架Controller
 */
@Api(tags = "书架模块")
@ApiSort(5)
@RestController
@RequestMapping("/lm_area")
public class BookRackController {

    @Autowired
    private BookRackService bookRackService;

    @GetMapping("/bookRack")
    @ApiOperation("通过区域id，获取所有的书架")
    @ApiOperationSupport(order = 511)
    public R findBookRackByAreaId(@Param("areaId") Integer areaId){
        List<LmBookRack> bookRacks = bookRackService.findBookRackByAreaId(areaId);
        return !bookRacks.isEmpty() ?R.ok().data("bookRacks",bookRacks) : R.error("数据为空");
    }
    @GetMapping("/bookRack/{id}")
    @ApiOperation("通过区域id，获取书架位置信息")
    @ApiOperationSupport(order = 513)
    public BookRackLocationDTO findBookRackLocationById(@PathVariable("id") Integer id){
        BookRackLocationDTO rackLocationDTO= bookRackService.findBookRackLocationById(id);
        return rackLocationDTO;
    }

    @GetMapping("/bookRack/search")
    @ApiOperation("搜索书架")
    @ApiOperationSupport(order = 512)
    public R search(LmBookRackSearch search){
        Map<String,Object> da = bookRackService.search(search);
        return R.ok().data(da);
    }

    @PostMapping("/bookRack")
    @ApiOperation("添加书架")
    @ApiOperationSupport(order = 521)
    public R addBookRack(@RequestBody LmBookRack bookRack){
        return bookRackService.addBookRack(bookRack) ? R.ok("添加书架成功!!!"):R.error("添加书架失败!!!");
    }


    @PutMapping("/bookRack")
    @ApiOperation("通过书架Id，修改书架")
    @ApiOperationSupport(order = 531)
    public R updateBookRack(@RequestBody LmBookRack bookRack){
        return bookRackService.updateBookRack(bookRack) ? R.ok("修改书架成功!!!"):R.error("修改书架失败!!!");
    }


    @DeleteMapping("/bookRack/{id}")
    @ApiOperation("通过书架Id，删除书架")
    @ApiOperationSupport(order = 541)
    public R deleteBookRackById(@PathVariable("id") Integer id){
        return bookRackService.deleteBookRackById(id) ? R.ok("删除书架成功!!!"):R.error("删除书架失败!!!");
    }
}
